package ca.nfb.media.nfbPlayer.ui
{
	import ca.nfb.media.nfbPlayer.views.NFBPlayerView;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import ca.turbulent.media.events.PyroEvent;
	
	
	public class HitZone extends NFBPlayerView
	{
		public static const MOUSED_OVER_EVENT		:String = "mousedOverEvent";
		public static const MOUSED_OUT_EVENT		:String = "mousedOutEvent";
		
		public static var GAP						:Number = 40;
		private var hitZoneHolder_mc				:MovieClip = new MovieClip();
		
		public function HitZone()
		{
			super();
		}
		
		override protected function addedToStage(evt:Event):void
		{
			registerStageEvents();
			addChild(hitZoneHolder_mc);
			drawHitZone();
			hitZoneHolder_mc.useHandCursor = true;
			hitZoneHolder_mc.buttonMode = true;
			hitZoneHolder_mc.addEventListener(MouseEvent.CLICK, clicked, false, 0, true);
			addMouseEvents();
		}
		
		private function drawHitZone():void
		{
			hitZoneHolder_mc.graphics.clear();
			hitZoneHolder_mc.graphics.beginFill(0x000000, 0)
			hitZoneHolder_mc.graphics.drawRect(GAP, GAP, stage.stageWidth-(GAP*2), stage.stageHeight-((GAP*1.5)+core.controllerHeight));
			hitZoneHolder_mc.graphics.endFill();
		}
		
		private function addMouseEvents():void
		{
			this.removeMouseEvents();
			hitZoneHolder_mc.addEventListener(MouseEvent.MOUSE_OVER, mousedOver, false, 0, true);
			hitZoneHolder_mc.addEventListener(MouseEvent.MOUSE_OUT, mousedOut, false, 0, true);
			
		}
		
		private function removeMouseEvents():void
		{
			if (hitZoneHolder_mc.hasEventListener(MouseEvent.MOUSE_OVER))
				hitZoneHolder_mc.removeEventListener(MouseEvent.MOUSE_OVER, mousedOver);
			
			if (hitZoneHolder_mc.hasEventListener(MouseEvent.MOUSE_OUT))
				hitZoneHolder_mc.removeEventListener(MouseEvent.MOUSE_OUT, mousedOut);
		}
		
		public function disable():void
		{
			removeMouseEvents();
		// 	hitZoneHolder_mc.mouseEnabled = false;
		}
		
		public function enable():void
		{
			addMouseEvents();
		// 	hitZoneHolder_mc.mouseEnabled = true;
		}
		
		private function clicked(evt:MouseEvent):void
		{
			if (core.currentState != NFBPlayer.MORE_INFOS_STATE && core.currentState != NFBPlayer.OPTIONS_STATE && core.currentState != NFBPlayer.SHARING_STATE)
			{
				player.togglePause();
			}
		}
		
		private function mousedOver(evt:MouseEvent):void
		{
			if (core.currentState != NFBPlayer.MORE_INFOS_STATE && core.currentState != NFBPlayer.OPTIONS_STATE && core.currentState != NFBPlayer.SHARING_STATE)
			{
				this.dispatchEvent(new Event(HitZone.MOUSED_OVER_EVENT));
			}
		
		}
		
		private function mousedOut(evt:MouseEvent):void
		{
			if (core.currentState != NFBPlayer.MORE_INFOS_STATE && core.currentState != NFBPlayer.OPTIONS_STATE && core.currentState != NFBPlayer.SHARING_STATE)
			{
				this.dispatchEvent(new Event(HitZone.MOUSED_OUT_EVENT));
			}
		}
		
		override protected function fullscreenHandler(evt:FullScreenEvent):void
		{
			drawHitZone();
		}
		
		override protected function sizeChanged(evt:PyroEvent=null):void
		{
			drawHitZone();
		}
		
		override protected function stageResized(evt:Event=null):void
		{
			drawHitZone();
		}
		
	}
}